<?php
/**
 * 搜窝
 *
 * @copyright 搜窝( www.souvo.com )
 * @package 前台用户登录
 * @version 1.0
 * @author wybwsk(wybwsk@qq.net)
 * @date 2011/5/2
 */
class MemcpAction extends FrontAction {
    protected function init() {
        if( !$this->user ) {
            //cookie('redirect_url',$this->getCurrentUrl());
            //echo cookie('redirect_url');
            alert('请先登录','',2);
            exit;
        }
        $this->assign('user',$this->user);
        $this->assign('user_info',$this->user_info);
    }
    /**
     *
     */
    public function _empty() {
        $this->infoSet();
    }

    public function comment() {
        if($this->post) {
            !$this->validTR(true) && exit('非法');
            $data['mid'] = $this->user['mid'];
            $data['fid'] = intval($_POST['fid']);
            $data['add_time'] = $data['update_time'] = time();
            $data['content'] = htmlspecialchars($_POST['content']);
            M('comment')->data($data)->add();
        }
        $comment=D('comment')->commentList($this->user['mid']);
        $this->display();
    }
    public function infoSet() {
        if($this->post) {
            !$this->validTR(true) && exit('非法');
            $update_data['nickname']=$update_data['province']=$update_data['city']=$update_data['qq']=$update_data['msn']=$update_data['gtalk']=$update_data['skype']=$update_data['yy']=NULL;
            $update_data['nickname'] = htmlspecialchars($_POST['nickname']);
            $nick_name_len = mb_strlen($update_data['nickname'],'UTF-8');
            if( $nick_name_len > 20 || $nick_name_len < 4) {
                alert('请输入4-20个字符','memcp/infoset','2');
                exit;
            }
            $update_data['province'] = intval($_POST['province']);
            $update_data['city'] = intval($_POST['city']);
            $update_data['qq'] = intval($_POST['qq']);
            $update_data['msn'] = intval($_POST['msn']);
            $update_data['gtalk'] = intval($_POST['gtalk']);
            $update_data['skype'] = intval($_POST['skype']);
            $update_data['yy'] = intval($_POST['yy']);
            $update_data['about'] = htmlspecialchars($_POST['about']);
            $update_data['update_time'] = time();
            if(M('member_info')->data($update_data)->where(array('mid'=>$this->user['mid']))->save()){
                alert('修改成功','memcp/infoset');
                exit;
            }
        }
        $data = M('member_info')->find($this->user['mid']);
        $pro_curr[$data['province']] = $city_curr[$data['city']] = ' selected="selected"';
        $this->assign('pro_curr',$pro_curr);
        $this->assign('city_curr',$city_curr);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 有BUG..图太小时不好使。先用着.
     */
    public function photoUpload() {
        if($this->post){
            import("ORG.Net.UploadFile");
            import("ORG.Util.Image");
            $upload = new UploadFile(5097152,'jpg,jpeg,bmp,png','image/jpeg,image/bmp,image/png','./resources/image/u/','time');
            $upload->autoSub = true;
            $upload->subType = 'date';
            $att_url = M('att')->find($this->user['mid']);
            $att_url = './resources/image/u/'.$att_url['large_img'];
            is_file($att_url) && unlink($att_url);
            $upload_info = $upload->uploadOne($_FILES['photo']);
            if(!$upload_info) {
                alert($upload->getErrorMsg(),'',2);
                exit;
            }else{
                $thumb_filename = './resources/image/u/'.$upload_info[0]['savename'];
                $thumb_new = pathinfo($upload_info[0]['savename']);
                $insert_data['large_img'] = $thumb_new['dirname'].'/'.$thumb_new['filename'].'_normal.'.$thumb_new['extension'];
                $img_preview = __APP__.'/resources/image/u/'. $insert_data['large_img'];
                $thumb_new = './resources/image/u/'.$insert_data['large_img'];
                $this->thumimage($thumb_filename,$thumb_new);
                @unlink($thumb_filename);
                $insert_data['mid'] = $this->user['mid'];
                M('att')->data($insert_data)->add(array(),array(),true);
                $this->assign('img_preview',$img_preview);
            }
        }
        if(empty($img_preview)) {
            $normal = M('att')->find($this->user['mid']);
            $this->assign('normal',$normal['large_img']);
        }
        $jcrop_js = $this->loadScript('jcrop,jcrop_func');
        $jcrop_css = $this->loadScript('jcrop',2);
        $this->assign('jcrop_js',$jcrop_js);
        $this->assign('jcrop_css',$jcrop_css);
        $this->display();
    }
    public function photoCropSave() {
        if($this->post) {
            if (!$this->validTR(true)){
                alert('非法','memcp/photoupload');
                exit();
            }
            $x = intval($_POST['x']);
            $y = intval($_POST['y']);
            $w = intval($_POST['w']);
            $h = intval($_POST['h']);
            $path=M('att')->find($this->user['mid']);
            $path = $path['large_img'];
            $save_path =pathinfo($path);
            $normal_path = './resources/image/u/'.$path;
            $large_save_path = $save_path['dirname'].'/'.substr($save_path['filename'],0,-7).'_large.'.$save_path['extension'];;
            $middle_save_path = $save_path['dirname'].'/'.substr($save_path['filename'],0,-7).'_middle.'.$save_path['extension'];
            $small_save_path = $save_path['dirname'].'/'.substr($save_path['filename'],0,-7).'_small.'.$save_path['extension'];
            $this->thumimg($x,$y,$w,$h,350,350,$normal_path,'./resources/image/u/'.$large_save_path);
            $this->thumimg($x,$y,$w,$h,160,160,$normal_path,'./resources/image/u/'.$middle_save_path);
            $this->thumimg($x,$y,$w,$h,75,75,$normal_path,'./resources/image/u/'.$small_save_path);
            $update_data['large_img'] = $large_save_path;
            $update_data['middle_img'] = $middle_save_path;
            $update_data['small_img'] = $small_save_path;
            M('member_info')->data($update_data)->where(array('mid'=>$this->user['mid']))->save();
            $this->redirect('memcp/photoupload');
        }
    }
    private function thumimg($x,$y,$w,$h,$width,$height,$path,$save) {
        $jpeg_quality = 100;
        $img = getimagesize($path);
        switch($img[2]){
            case 1:
                $img_r=@imagecreatefromgif($path);//载入图片，创建新图片
                break;
            case 2:
                $img_r=@imagecreatefromjpeg($path);
                break;
            case 3:
                $img_r=@imagecreatefrompng($path);
                break;
        }
        $dst_r = imageCreateTrueColor($width,$height);
        imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$width,$height,$w,$h);
        imagejpeg($dst_r,$save,$jpeg_quality);
        imagedestroy($dst_r);
        imagedestroy($img_r);
    }
    /**
     *　定宽抓图;
     */
    private function thumimage($path,$save){
        $img = getimagesize($path);
        $width_y=$img[0];
        $height_y=$img[1];
        if($width_y > 600){
            $width=600;
            $height=intval(round($height_y/($width_y/600)));
        }else if($height_y>600){
            $width=intval(round($width_y/($height_y/600)));
            $height=600;
        }
        if($width_y > 600 || $height_y > 600){
            $dst_r = imagecreatetruecolor($width,$height);
            switch($img[2]){
                case 1:
                    $img_r=@imagecreatefromgif($path);//载入图片，创建新图片
                    break;
                case 2:
                    $img_r=@imagecreatefromjpeg($path);
                    break;
                case 3:
                    $img_r=@imagecreatefrompng($path);
                    break;
            }
            imagecopyresampled($dst_r,$img_r,0,0,0,0,$width,$height,$width_y,$height_y);
            $re = imagejpeg($dst_r,$save,100);
        }
        return $re;
    }

    public function collect() {
        $cgid = intval($_GET['cgid']);
        $condition = array('mid'=>$this->user['mid']);
        $collect = $this->getPageData(M('collect'),20,$condition,'`cid` DESC');
        foreach($collect['data_list'] as $value){
            $collect_mid .= $value['collect_mid'].',';
        }
        $collect_mid = trim($collect_mid,',');
        $tmp_data = M('member_info')->field('`mid`,`nickname`,`small_img`')->where('`mid` IN ('.$collect_mid.')')->findAll();
        foreach ($tmp_data as $value){
            $tmp_collect_member[$value['mid']] = $value;
        }
        $tmp_tag = M('tag')->where('`mid` IN ('.$collect_mid.')')->findAll();
        foreach($tmp_tag as $value){
            $tmp_collect_member[$value['mid']]['tag'][] = (array)$value;
        }

        //排序
        foreach($collect['data_list'] as $value){
            $collect_member[] = $tmp_collect_member[$value['collect_mid']];
        }
        unset($collect_mid,$value,$tmp_data,$tmp_collect_member,$tmp_tag);
        $collect_group = M('collect_group')->where(array('mid'=>$this->user['mid']))->findAll();
        $this->assign('collect',$collect_member);
        $this->assign('page',$collect['page_list']);
        $this->assign('collect_group',$collect_group);
        $this->display();
    }

    public function collectGroupAdd() {
        if($this->ajax) {
            $data['collect_group_name'] = htmlspecialchars($_POST['collect_group_name']);
            empty($data['collect_group_name']) && exit('2');
            $data['mid'] = $this->user['mid'];
            $cgid = M('collect_group')->data($data)->add();
            if($cgid) {
                $re = array('msg_a'=>'<dd><q></q><a href="'.U('memcp/collect',array('cgid'=>$cgid)).'" title="'.$data['collect_group_name'].'">'.$data['collect_group_name'].'</a>(0)</dd>',
                    'msg_b'=>'<li>'.$data['collect_group_name'].'</li>');
                echo json_encode($re);
            }else{
                exit('1');
            }
        }
    }

    public function collectGroupUpdate() {
    }
    public function collectGroupdel() {
    }

    public function linkSet() {
        $link_type = M('link_type')->order('`order` DESC')->findALL();
        $data = M('link')->where(array('mid'=>$this->user['mid']))->findAll();
        $this->assign('data',$data);
        $this->assign('link_type',$link_type);
        $this->display();
    }

    public function linkUpdate() {
        if($this->post && $this->ajax) {
            !$this->validTR(true) && exit('1');
            $ltid = intval($_POST['ltid']);
            $ltid === 0 && exit('1');
            $link_type = M('link_type')->find($ltid);
            $update_data['url'] = htmlspecialchars($_POST['url']);
            $rule = $link_type['rule'];
            //将自定义的链接类型排除
            $rule = ($ltid != 1) ? strpos($update_data['url'],$link_type['rule']) : true;
            if($rule===0 || $rule) {
                $update_data['mid'] = $this->user['mid'];
                empty($update_data['url']) && exit('2');
                $update_data['url'] = ( strpos($update_data['url'],'http://') !== false ) ? $update_data['url'] : 'http://'.$update_data['url'];
                /*验证ＵＲＬ有问题，http和https....FUCK 正则.*/
                $update_data['ico'] = $link_type['ico'];
                $update_data['tname'] = $link_type['tname'];
                $update_data['ltid'] = $ltid;
                $update_data['addtime'] = time();
                $update_data['lid'] = intval($_POST['lid']);
                if(M('link')->where(array('lid'=>$update_data['lid'],'mid'=>$this->user['mid']))->findAll()) {
                    M('link')->data($update_data)->where(array('lid'=>$update_data['lid']))->save();
                    exit('5');
                }else{
                    exit('1');
                }
            }else{
                exit('3');
            }
        }else{
            exit('1');
        }

    }
    public function linkDel() {
        if($this->post && $this->ajax) {
            !$this->validTR(true) && exit('1');
            $lid = intval($_POST['lid']);
            $lid === 0 && exit('1');
            if(M('link')->where(array('lid'=>$lid,'mid'=>$this->user['mid']))->findAll()) {
                M('link')->where(array('lid'=>$lid))->delete();
                exit('2');
            }
            exit('1');
        }else{
            exit('1');
        }
    }
    public function linkAdd() {
        if($this->post) {
            !$this->validTR(true) && exit('非法');
            $ltid = intval($_POST['ltid']);
            if($ltid === 0) {
                alert('非法','',2);
                exit;
            }
            $link_type = M('link_type')->find($ltid);
            $insert_data['url'] = htmlspecialchars($_POST['url']);
            $rule = $link_type['rule'];
            //将自定义的链接类型排除
            $rule = ($ltid != 1) ? strpos($insert_data['url'],$link_type['rule']) : true;
            if($rule===0 || $rule) {
                $insert_data['mid'] = $this->user['mid'];
                if(empty($insert_data['url'])) {
                    alert('这链接能链到哪?','',2);
                    exit;
                }
                $insert_data['url'] = ( strpos($insert_data['url'],'http://') !== false ) ? $insert_data['url'] : 'http://'.$insert_data['url'];
                /*验证ＵＲＬ有问题，http和https....FUCK 正则.*/

                $insert_data['ico'] = $link_type['ico'];
                $insert_data['tname'] = $link_type['tname'];
                $insert_data['ltid'] = $ltid;
                $insert_data['addtime'] = time();
                M('link')->data($insert_data)->add();
                alert('添加成功','memcp/linkset');
                exit;
            }else{
                alert('请选择正确的类型','',2);
                exit;
            }
        }
    }
    public function siteSms() {
        $db = M('top_sitesms');
        //查主留言;
        $condition = array('mid'=>$this->user['mid']);
        $order = array('update_time'=>'DESC');
        $data = $this->getPageData($db,25,$condition,$order);
        $this->assign('data',$data['data_list']);
        $this->assign('page',$data['page_list']);
        $this->display();
    }

    public function replySms() {
        $tsid = intval($_POST['tsid']);
        if($tsid == 0) {
            alert('非法','',2);
            exit();
        }
        if($this->post) {
            if(!$this->validTR(true)) {
                alert('非法','',2);
                exit();
            }
            $insert_data['tsid'] = $tsid;
            $insert_data['content'] = htmlspecialchars($_POST['content']);
            $insert_data['add_time'] = time();
            $insert_data['sse_mid'] = $this->user_info['mid'];
            $insert_data['sse_nickname'] = $this->user_info['nickname'];
            $sub_db = M('sub_sitesms');
            $sub_db->data($insert_data)->add();
            $top_db = M('top_sitesms');
            $top_sms = $top_db->find($tsid);
            $insert_data['tsid'] = $top_sms['relation_tsid'];
            $sub_db->data($insert_data)->add();
            $top_db->data(array('rs_type'=>1,'update_time'=>time()))->where(array('tsid'=>$tsid))->save();
            $top_db->data(array('rs_type'=>3,'update_time'=>time()))->where(array('tsid'=>$top_sms['relation_tsid']))->save();


        }
        alert('回复成功','memcp/sitesmsshow',$type="1",array('tsid'=>$tsid));
    }

    public function siteSmsShow() {
        $tsid = intval($_GET['tsid']);
        if($tsid == 0) {
            alert('非法','',2);
            exit();
        }
        $db = M('top_sitesms');
        $top_data = $db->find($tsid);
        if($top_data['rs_type'] ==3 || $top_data['rs_type'] ==4) {
            $db->where(array('tsid'=>$top_data['relation_tsid']))->data(array('rs_type'=>2))->save();
            $db->where(array('tsid'=>$top_data['tsid']))->data(array('rs_type'=>4))->save();
        }
        $sub_data = M('sub_sitesms')->where(array('tsid'=>$tsid))->findAll();
        $this->assign('top_data',$top_data);
        $this->assign('sub_data',$sub_data);
        $this->display();
    }
}